using Progress.Lang.*.

routine-level on error undo, throw.

using OpenEdge.WebSpeed.Request from propath.
using OpenEdge.WebSpeed.Response from propath.
using OpenEdge.WebSpeed.Session from propath.
using OpenEdge.WebSpeed.AgentSettings from propath.
using OpenEdge.WebSpeed.WebContext from propath.
using OpenEdge.WebSpeed.WebObject from propath.
using OpenEdge.WebSpeed.LegacyWebObject from propath.


class OpenEdge.WebSpeed.Container: 
    
    
    define public property ApplicationURL            as character no-undo
        get.
        private set.        
    
    define public property CookieDomain as character no-undo
        get.
        private set.
    define public property CookiePath as character no-undo
        get.
        private set.    
    
    define public property StateAware as logical no-undo
        get.
        private set.

    define public property AgentSettings as AgentSettings no-undo
        get.
        private set.
        
    define public property WebContext as WebContext no-undo
        get.
        private set.
        
    define public property RootURL as character no-undo init "":U 
        get.
        private set.
        
    /**
      returns true if the server is in developer mode
    */
    
    define public property DevelopmentMode as logical
        get.
        private set.
    
    define public property ServerAppMode as character no-undo
        get.
        private set.
    	
    define public property EvaluationMode as logical no-undo
        get.
        private set.
    	
    define private property ServerDebugging as character no-undo
        get.
        private set.
    	
    define public property DebuggingEnabled as logical no-undo
        get.
        private set.

    	
		
    constructor Container ():
    
    end constructor.    	

    /**
        Description: Initializes PROGRESS session variables from the environment. 
        Input:       <none>
        Output:      Sets global variables defined in src/web/method/cgidefs.i
        Notes:       These values should be the default values on a WEB-based client.
                   (But it never hurts to make sure.)    
    
    */
    method public void InitContainer():
        this-object:WebContext = new WebContext().
        this-object:AgentSettings = new AgentSettings().
        this-object:AgentSettings:InitAgentSettings().
        
        define variable cValue as character no-undo.
	
        /* Never pause for user input. */
        pause 0 before-hide.
        session:system-alert-boxes = false.
        session:appl-alert-boxes   = false.
	 
        /* Get configuration settings from ubroker.properties */
        this-object:ServerAppMode       = this-object:WebContext:get-config-value("srvrAppMode":U).
        this-object:DevelopmentMode     = ServerAppMode begins "Dev":U.
        this-object:EvaluationMode      = CheckAgentMode("Evaluation":U).
        this-object:ServerDebugging     = this-object:WebContext:get-config-value("srvrDebug":U). 
        this-object:ApplicationURL		= this-object:WebContext:get-config-value("applicationURL":U).
        this-object:CookiePath   		= this-object:WebContext:get-config-value("defaultCookiePath":U).
        this-object:CookieDomain 		= this-object:WebContext:get-config-value("defaultCookieDomain":U).
        this-object:RootURL          	= this-object:WebContext:get-config-value("wsRoot":U).
	
        /* If in Production mode and debugging is not enabled or debugging is
           disabled, then set flag to disable debugging. */
        if (CheckAgentMode("Production":U) and 
            not can-do(this-object:ServerDebugging, "Enabled":U)) or
            can-do(this-object:ServerDebugging, "Disabled":U)       or
            can-do(this-object:ServerDebugging, "Off":U) then
            this-object:DebuggingEnabled = false.
        else
            this-object:DebuggingEnabled = true.
		      
        this-object:StateAware = (this-object:AgentSettings:GetAgentSetting("Session":U, "", "StateAware":U) = "yes":U).
        

    /*        /* State-aware support if not explicitly refused or is missing (for backward*/
    /*           compatability). */                                                       */
    /*        IF OS-GETENV("STATE_AWARE_ENABLED":U) <> "no":U THEN                        */
    /*            cSuperStack = "web/objects/stateaware.p," + cSuperStack.                */
    /*                                                                                    */
    /*        /* Handles development propath modifications, this only functions           */
    /*           in a development environment. */                                         */
    /*        IF OS-GETENV("MULTI_DEV_PROPATH":U) > "" THEN                               */
    /*            cSuperStack = cSuperStack + ",web/objects/devpath.p".                   */
    /*                                                                                    */
    /*        /* Handles logging of individual web requests. */                           */
    /*        IF OS-GETENV("LOG_TYPES":U) > "" THEN                                       */
    /*            cSuperStack = cSuperStack + ",web/objects/runlog.p".                    */
    /*                                                                                    */
    /*        /* Add DBTools. Has methods to handle all your DB needs */                  */
    /*        IF OS-GETENV("DATABASES":U) > "" THEN                                       */
    /*            cSuperStack = cSuperStack + ",web/objects/dbtools.p".                   */
    /*                                                                                    */
    /*        /* File-based Session management. */                                        */
    /*        cSuperStack = cSuperStack + ",web/objects/session.p".                       */
    /*                                                               */
    /*        IF (SESSION:ICFPARAMETER > "") THEN                    */
    /*            /* Start the Dynamics environment. */              */
    /*            cSuperStack = cSuperStack + ",ry/app/rywebspeed.p".*/
    /*                                                                               */
    /*        /* Handles customized SUPER functionality.  Adds Super functionality to*/
    /*           init-session, init-request, end-request, if specified. */           */
    /*        IF OS-GETENV("SUPER_PROC":U) > "" THEN                                 */
    /*            cSuperStack = cSuperStack + "," + OS-GETENV("SUPER_PROC":U).       */
    /*                                                                  */
    /*        setSuperStack(cSuperStack). /** Start super procedures **/*/
    /*        RUN init-config.   /** Set environment based configuration variables (allows override before init-session) **/*/
    /*        RUN init-session.  /** Init-session **/                                                                       */
    
    end.
    
    /**
       called from webdisp to handle the webspeed main event. 
           
       This handles the main processing loop by creating new request and response object,
       calling the web object and cleaning up
        
    */
    method public void WebNotify().
	
	
        define variable request    as Request  no-undo.
        define variable response   as Response no-undo.
        define variable webSession as Session  no-undo.
	   
	   
        request = new Request(this-object).

        response = new Response(this-object, request).
        
        webSession = new Session().
        
        request:InitRequest(response, webSession).
		
        response:InitResponse().
		
        this-object:RunWebObject(request, response).
		
        finally:
            if valid-object(response) then 
            do:
                response:EndResponse().
                delete object response.
            end.

            if valid-object(request) then 
            do:
                request:EndRequest().
                delete object request.
            end.
            
            if valid-object(webSession) then 
            do:
                delete object webSession.
            end.
	      
        end finally.    
    
    end method.
	
    method public void RunWebObject(input request as Request, input response as Response):
		
        define variable webObject as WebObject no-undo.
		
        do on error undo, throw:
            webObject = new LegacyWebObject(request, response).
            webObject:BeginRequest().
            webObject:HandleRequest().
            webObject:EndRequest().
			
            catch e as Progress.Lang.Error:
                response:ShowErrorScreen(substitute("A problem was encountered while running &1", request:AppProgram)).
                return.
            end.

            finally:
                if valid-object(webObject) then 
                do:
                    delete object webObject.
                end.
            end.				
        end.
		
    end method.
	
    method public void Dispose().
	
    end.
    
    
    /**
      returns true if the agent is configured for the given mode
    */
    method public logical CheckAgentMode 
        ( input p_mode as character ) :
        return can-do(this-object:ServerAppMode, p_mode).
	
    end.    
    
end class.
